package cn.zb.record;

import cn.zb.common.BaseController;
import cn.zb.user.User;
import cn.zb.user.UserInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

/**
 * Created by Yang on 2015/3/10.
 */
@Before(UserInterceptor.class)
public class RecordController extends BaseController {

    public void index() {
        User user = getSessionAttr(ZB_ADMIN_USER);
        Page<Record> page = Record.me.page(getParaToInt("pageNo", 1), getParaToInt("pageSize", 10), user.getInt("id"));
        setAttr("page", page);
        setAttr("totalPrice", Record.me.totalPrice(user.getInt("id")));
        render("index.vm");
    }

    public void add() {}

    public void save() {
        getModel(Record.class)
                .set("author_id", ((User) getSessionAttr(ZB_ADMIN_USER)).get("id"))
//                .set("author", ((User) getSessionAttr(ZB_ADMIN_USER)).get("name"))
                .set("inTime", new Date())
                .save();
        redirect("/record");
    }

    public void delete() {
        getModel(Record.class).deleteById(getParaToInt("id"));
        redirect("/record?pageNo=" + getPara("pageNo"));
    }
}
